<script setup>
	import { onMounted, ref, getCurrentInstance, defineProps } from 'vue'
	import restApi from '@/api/rest/restApi'
	import ParamDefine from '@/views/restapi/ParamDefine.vue'
	import ResultWrapper from '@/views/restapi/ResultWrapper.vue'
	import HeaderConfig from '@/views/restapi/HeaderConfig.vue'
	import { nanoid } from 'nanoid'
	import ProcessRelationConfig from '@/views/process/ProcessRelationConfig.vue'
	import Group from '@/components/Group/index.vue'
	import Detail from '@/views/restapi/detail.vue'

	// let props = defineProps(['hiddenFlag', 'operaCallback'])
	const props = defineProps({
		hiddenFlag: {
			type: Boolean,
			default: false
		},
		productId: {
			type: String,
			default: ''
		},
		operaCallback: {
			type: Function
		}
	})
	const productId = ref()
	const currentAction = ref()
	onMounted(() => {
		productId.value = props.productId
		if (!productId.value) {
			productId.value = proxy.$route.query.id
		}
		queryApiList()
	})
	const addApiFlag = ref(false)
	const { proxy } = getCurrentInstance()
	const apiModel = ref({})
	const saveApi = async () => {
		apiModel.value.groupId = currentGroupId.value
		apiModel.value.productId = productId.value
		let id = await restApi.saveApiConfig(apiModel.value)
		await queryApiList()
		if (props.operaCallback) {
			props.operaCallback(currentAction.value, 'api', {
				id: id,
				...apiModel.value
			})
		}
		addApiFlag.value = false
		apiModel.value = {}
	}
	const apiList = ref([])
	const queryApiList = async () => {
		if (props.hiddenFlag) {
			return
		}
		const list = await restApi.apiList({
			groupId: currentGroupId.value == 0 ? null : currentGroupId.value,
			productId: proxy.$route.query.id
		})
		apiList.value = list
	}

	const apiId = ref()
	const apiDetailFlag = ref(false)
	const renderRight = async (row) => {
		apiDetailFlag.value = true;
		apiId.value = row.id;
		console.log(row)
	}
	const offOnline = async (apiConfig) => {
		if (!apiConfig.apiStatusCode || apiConfig.apiStatusCode == 'OFFLINE') {
			await restApi.updateApiStatusCode(apiConfig.id, 'ONLINE')
			apiConfig.apiStatusCode = 'ONLINE'
		} else if (apiConfig.apiStatusCode == 'ONLINE') {
			await restApi.updateApiStatusCode(apiConfig.id, 'OFFLINE')
			apiConfig.apiStatusCode = 'OFFLINE'
		}
		await queryApiList()

		if (props.operaCallback) {
			console.log(apiConfig)
			props.operaCallback('offline', 'api', {
				...apiConfig
			})
		}
	}

	const bizId = ref()

	const currentGroupId = ref(0)

	const selectTree = (id, code, name) => {
		currentGroupId.value = id
		queryApiList()
	}

	const remove = async (data) => {
		await restApi.removeById(data.id)
		if (props.operaCallback) {
			props.operaCallback('delete', 'api', data)
		}
	}

	const edit = async (data) => {
		let c = await restApi.queryApiById(data.id)
		apiModel.value = c
		addApiFlag.value = true
	}
	const show = (action, data) => {
		currentAction.value = action
		if (action == 'add') {
			addApiFlag.value = true
			currentGroupId.value = data.id
		} else if (action == 'remove') {
			remove(data)
		} else if (action == 'edit') {
			currentGroupId.value = data.groupId
			edit(data)
		} else if (action == 'publish') {
			offOnline(data)
		}
	}

	defineExpose({
		show
	})
</script>

<template>
	<a-row :gutter="10" :body-style="{ 'padding-bottom': '0px' }" class="mb-2" v-if="!props.hiddenFlag">
		<a-col :xs="24" :sm="24" :md="24" :lg="5" :xl="5">
			<Group
				height="calc(73vh)"
				:hiddenFlag="false"
				relationTypeCode="SCENE"
				:productId="productId"
				:selectTree="selectTree"
			></Group>
		</a-col>
		<a-col :xs="24" :sm="24" :md="24" :lg="19" :xl="19">
			<a-row :gutter="10" :body-style="{ 'padding-bottom': '0px' }" class="mb-2">
				<a-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
					<a-card>
						<a-space>
							<a-button @click="queryApiList">刷新</a-button>
							<a-button type="primary" @click="addApiFlag = true">新增</a-button>
						</a-space>
					</a-card>
				</a-col>
			</a-row>

			<a-row :gutter="10" :body-style="{ 'padding-bottom': '0px' }" class="mb-2">
				<a-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
					<a-space>
						<a-card v-for="api in apiList" class="box-card" v-bind:key="api.id" style="width: 200px; height: 200px">
							<template #title>
								<a-space>
									<a-tag @click="renderRight(api)">{{ api.apiName }}</a-tag>
									<span>{{ api.apiCode }}</span>
								</a-space>
							</template>

							<a-row :gutter="10" :body-style="{ 'padding-bottom': '0px' }" class="mb-2">
								<a-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
									{{ api.description }}
								</a-col>
							</a-row>
							<a-row :gutter="10" style="margin-top: 60px" :body-style="{ 'padding-bottom': '0px' }" class="mb-2">
								<a-button :type="api.apiStatusCode == 'ONLINE' ? 'primary' : 'primary'" @click="offOnline(api)"
									>{{ api.apiStatusCode == 'ONLINE' ? '下线' : '上线' }}
								</a-button>
							</a-row>
						</a-card>
					</a-space>
				</a-col>
			</a-row>
		</a-col>
	</a-row>

	<a-modal :closable="false" v-model:open="addApiFlag">
		<a-form :model="apiModel">
			<a-form-item label="api名称">
				<a-input v-model:value="apiModel.apiName"></a-input>
			</a-form-item>
			<a-form-item label="api编码">
				<a-input :disabled="apiModel.id != null" v-model:value="apiModel.apiCode"></a-input>
			</a-form-item>
			<a-form-item label="api描述">
				<a-input v-model:value="apiModel.description"></a-input>
			</a-form-item>
		</a-form>
		<template #footer>
			<a-button @click="addApiFlag = false">取 消</a-button>
			<a-button type="primary" @click="saveApi">确 定</a-button>
		</template>
	</a-modal>

	<a-modal
		v-model:open="apiDetailFlag"
		@cancel="apiDetailFlag = false"
		@ok="updateApi"
		:footer="null"
		:closable="false"
		:width="1200"
	>
		<Detail :id="apiId"></Detail>
	</a-modal>
</template>

<style scoped></style>
